package edu.unlv.cs.ladders.entities;

import edu.unlv.cs.ladders.entities.Team;
import edu.unlv.cs.ladders.entities.TournamentDataOnDemand;
import java.security.SecureRandom;
import java.util.List;
import java.util.Random;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

privileged aspect TeamDataOnDemand_Roo_DataOnDemand {
    
    declare @type: TeamDataOnDemand: @Component;
    
    private Random TeamDataOnDemand.rnd = new SecureRandom();
    
    private List<Team> TeamDataOnDemand.data;
    
    @Autowired
    private TournamentDataOnDemand TeamDataOnDemand.tournamentDataOnDemand;
    
    public Team TeamDataOnDemand.getNewTransientTeam(int index) {
        edu.unlv.cs.ladders.entities.Team obj = new edu.unlv.cs.ladders.entities.Team();
        obj.setCode("code_" + index);
        obj.setCurrentStanding(new Integer(index));
        obj.setHasPaid(new Boolean(true));
        obj.setName("name_" + index);
        obj.setRank(new Integer(index));
        obj.setTournament(tournamentDataOnDemand.getRandomTournament());
        return obj;
    }
    
    public Team TeamDataOnDemand.getSpecificTeam(int index) {
        init();
        if (index < 0) index = 0;
        if (index > (data.size()-1)) index = data.size() - 1;
        Team obj = data.get(index);
        return Team.findTeam(obj.getId());
    }
    
    public Team TeamDataOnDemand.getRandomTeam() {
        init();
        Team obj = data.get(rnd.nextInt(data.size()));
        return Team.findTeam(obj.getId());
    }
    
    public boolean TeamDataOnDemand.modifyTeam(Team obj) {
        return false;
    }
    
    @Transactional(propagation = Propagation.REQUIRES_NEW)
    public void TeamDataOnDemand.init() {
        if (data != null) {
            return;
        }
        
        data = edu.unlv.cs.ladders.entities.Team.findTeamEntries(0, 10);
        if (data == null) throw new IllegalStateException("Find entries implementation for 'Team' illegally returned null");
        if (data.size() > 0) {
            return;
        }
        
        data = new java.util.ArrayList<edu.unlv.cs.ladders.entities.Team>();
        for (int i = 0; i < 10; i++) {
            edu.unlv.cs.ladders.entities.Team obj = getNewTransientTeam(i);
            obj.persist();
            data.add(obj);
        }
    }
    
}
